<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="企业名称" prop="entName">
        <el-input
          v-model="queryParams.entName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业类型">
        <el-select v-model="queryParams.entType" placeholder="请选择企业类型" clearable>
          <el-option
            v-for="dict in dict.type.wms_ent_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商类型">
        <el-select v-model="queryParams.gysType" placeholder="请选择供应商类型" clearable>
          <el-option
            v-for="dict in dict.type.wms_ent_gys_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台类型">
        <el-select v-model="queryParams.platformType" placeholder="请选择平台类型" clearable>
          <el-option
            v-for="dict in dict.type.wms_ent_platform_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.wms_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <div style="display: flex;margin-right: 10px;float: left">
        <div class="addressTitle">
          所在地
        </div>
        <div style="flex: 1;">
          <treeselect v-model="queryParams.city" :options="cityOptions" :show-count="true" placeholder="请选所在地"  style="width: 200px"/>
        </div>
      </div>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:ent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:ent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:ent:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="entList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="entName" :show-overflow-tooltip="true"/>
      <el-table-column label="企业类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.entType === '0'">平台企业</el-tag>
          <el-tag type="warning" v-else-if="scope.row.entType === '1'">供应商</el-tag>
          <el-tag type="success" v-else-if="scope.row.entType === '2'">客户</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="供应商类型" align="center" prop="">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gysType === '0'">服务类</el-tag>
          <el-tag type="success" v-else-if="scope.row.gysType === '1'">物资类</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="平台类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.platformType === '0'">期限</el-tag>
          <el-tag type="success" v-else-if="scope.row.platformType === '1'">永久</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="到期日期" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.platformType && scope.row.platformType === '0'">{{scope.row.contractDate}}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="最近一次合作日期" align="center" prop="endTime" />
      <el-table-column label="合作次数" align="center" prop="hzCount" />
      <el-table-column label="所属在地" align="center" prop="cityName" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
        <el-tag v-if="scope.row.status === '0'">启用</el-tag>
        <el-tag type="danger" v-else-if="scope.row.status === '1'">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:ent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:ent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="企业名称" prop="entName">
          <el-input v-model="form.entName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业类型">
          <el-radio-group v-model="form.entType" @change="handleEntTypeChange">
            <el-radio
              v-for="dict in dict.type.wms_ent_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="供应商类型" v-if="form.entType === '1'">
          <el-radio-group v-model="form.gysType">
            <el-radio
              v-for="dict in dict.type.wms_ent_gys_type"
              :key="dict.value"
              :label="dict.value"
              :disabled="userId !== 1 && dict.valueOf === '0'"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="平台类型" v-if="form.entType === '0'">
          <el-radio-group v-model="form.platformType" @change="handelChange">
            <el-radio
              v-for="dict in dict.type.wms_ent_platform_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="到期时间" v-if="form.platformType === '0'" prop="contractDate">
          <el-date-picker
            v-model="form.contractDate"
            type="date"
            placeholder="选择日期" style="width: 100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所在地">
          <treeselect v-model="form.city" :options="cityOptions" :show-count="true" placeholder="请选所在地" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.wms_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-loading="btnLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEnt, getEnt, delEnt, addEnt, updateEnt } from "@/api/wms/ent";
import { listCityTree } from "@/api/wms/city";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Ent",
  components: {Treeselect},
  dicts: ['wms_ent_type', 'wms_ent_gys_type', 'wms_status', 'wms_ent_platform_type'],
  data() {
    return {
      //按钮加载
      btnLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      cityOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      entList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entType: null,
        gysType: null,
        city: null,
        entName: null,
        status: null,
        platformType: null
      },
      //用户id
      userId: null,
      // 部门树选项
      deptOptions: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        entName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
          { max: 50, message: '企业名称长度最多 50个字', trigger: 'blur' }
        ],
        contractDate:[
          { required: true, message: "请选择到期时间", trigger: "blur" },
        ]
      }
    };
  },
  created() {
    this.userId = this.$store.getters.id
    this.getList();
    this.getCity();
  },
  methods: {
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listEnt(this.queryParams).then(response => {
        this.entList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCity(){
      listCityTree().then(response => {
        this.cityOptions = response.data;
      })
    },
    //企业类型变更事件
    handleEntTypeChange(val, platformType, gysType) {
      // 企业类型是平台企业初始化设置平台企业类型为期限并且增加表单验证，否则清空
      if (val == '0') {
        if (!platformType) {
          this.rules.contractDate=[
            { required: true, message: "请选择到期时间", trigger: "blur" },
          ]
          this.form.platformType='0'
        }
      } else {
        this.form.platformType=null
        this.rules.contractDate=[]
        this.form.contractDate=null
      }

      // 企业类型是供应商初始化设置供应商类型为物资类，否则清空
      if (!gysType) {
        if (val != '1') {
          this.form.gysType = null
        } else {
          this.form.gysType='0'
        }
      }
    },
    //平台类型变更事件
    handelChange(val){
      if (val == "0"){
        this.rules.contractDate=[
          { required: true, message: "请选择到期时间", trigger: "blur" },
        ]
      } else {
        this.rules.contractDate=[]
        this.form.contractDate=null
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entType: '0',
        gysType: null,
        city: null,
        entName: null,
        status: '0',
        platformType: '0',
        contractDate: null
      };
      if (this.userId !== 1){
        this.form.entType = '1'
        this.form.gysType = '0'
        this.form.platformType = null
        this.rules.contractDate=[]
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.city = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEnt(id).then(response => {
        this.form = response.data;
        this.handleEntTypeChange(this.form.entType, this.form.platformType, this.form.gysType);
        if (this.form.platformType) {
          this.handelChange(this.form.platformType)
        }
        this.open = true;
        this.title = "修改企业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.btnLoading = true
          if (this.form.id != null) {
            updateEnt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(f => {
              this.btnLoading = false
            });
          } else {
            addEnt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(f => {
              this.btnLoading = false
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业编号为"' + ids + '"的数据项？').then(function() {
        return delEnt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    /** handleExport() {
      this.download('wms/ent/export', {
        ...this.queryParams
      }, `ent_${new Date().getTime()}.xlsx`)
    } */
  }
};
</script>

<style scoped>
.addressTitle{
  width: 90px;
  text-align: right;
  padding-right: 12px;
  line-height: 32px;
  font-size: 14px;
  color: #606266;
  font-weight: bold;
}
/deep/ .el-form-item--medium .el-form-item__content{
  line-height: 32px !important;
}
</style>
